<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogProps.title"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
    width="800px"
    top="5vh"
    class="work-sum-dialog"
  >
    <el-form
      ref="ruleFormRef"
      :model="dialogProps.row"
      :rules="rules"
      label-width="100px"
      label-suffix=" :"
      label-position="right"
      status-icon
    >
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="总结标题" prop="title">
            <el-input
              v-model="dialogProps.row!.title"
              placeholder="请输入总结标题"
              :disabled="dialogProps.isView"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="总结日期" prop="workDate">
            <el-date-picker
              v-model="dialogProps.row!.workDate"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择总结日期"
              :disabled="dialogProps.isView"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="工作总结" prop="summary">
            <el-input
              v-model="dialogProps.row!.summary"
              :rows="6"
              type="textarea"
              placeholder="请输入工作总结"
              :disabled="dialogProps.isView"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="待提升" prop="improvement">
            <el-input
              v-model="dialogProps.row!.improvement"
              :rows="6"
              type="textarea"
              placeholder="请输入待提升内容"
              :disabled="dialogProps.isView"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button @click="handleClose"> 取消 </el-button>
      <el-button v-if="!dialogProps.isView" type="primary" @click="handleSubmit" :loading="loading"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="tsx" name="workSumDialog">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { WorkSum } from "./types/worksum";
import { useUserStore } from "@/stores/modules/user";

const loading = ref(false);
const ruleFormRef = ref<FormInstance>();
const dialogVisible = ref(false);

const dialogProps = reactive({
  title: "",
  isView: false,
  tbName: "",
  row: {} as Partial<WorkSum>,
  api: undefined as any,
  getTableList: undefined as any
});

// 表单验证规则
const rules = reactive({
  title: [{ required: true, message: "请输入总结标题", trigger: "blur" }],
  workDate: [{ required: true, message: "请选择总结日期", trigger: "change" }],
  summary: [{ required: true, message: "请输入工作总结", trigger: "blur" }],
  improvement: [{ required: true, message: "请输入待提升内容", trigger: "blur" }]
});

// 接收父组件参数
const acceptParams = (params: any) => {
  Object.assign(dialogProps, params);
  // 如果是新增，设置默认日期为今天
  if (params.title === "新增") {
    dialogProps.row.workDate = new Date().toISOString().slice(0, 10);
  }
  dialogVisible.value = true;
};

// 关闭弹窗
const handleClose = () => {
  dialogVisible.value = false;
  ruleFormRef.value?.resetFields();
  dialogProps.row = {} as Partial<WorkSum>;
};

// 提交数据
const handleSubmit = () => {
  ruleFormRef.value?.validate(async valid => {
    if (!valid) return;
    loading.value = true;

    try {
      // 如果是新增
      if (dialogProps.title === "新增") {
        dialogProps.row.create_at = new Date();
        dialogProps.row.create_user = useUserStore().userInfo._id;
        dialogProps.row.userId = useUserStore().userInfo._id;
      }

      await dialogProps.api(dialogProps.tbName, dialogProps.row);
      ElMessage.success(`${dialogProps.title}工作总结成功！`);
      dialogProps.getTableList && dialogProps.getTableList();
      handleClose();
    } catch (error) {
      console.error(error);
      ElMessage.error(`${dialogProps.title}工作总结失败！`);
    } finally {
      loading.value = false;
    }
  });
};

defineExpose({
  acceptParams
});
</script>

<style lang="scss" scoped>
.work-sum-dialog {
  :deep(.el-textarea__inner) {
    font-family: inherit;
  }
}
</style>
